package com.kaili.basic.service.impl;

import com.baomidou.mybatisplus.plugins.Page;
import com.kaili.basic.mapper.GroupMapper;
import com.kaili.basic.mapper.PushGroupMapper;
import com.kaili.basic.pojo.dto.*;
import com.kaili.basic.pojo.entity.MdmGroupTree;
import com.kaili.basic.pojo.entity.MdmOrgEntity;
import com.kaili.basic.pojo.entity.MdmPushGroupEntity;
import com.kaili.basic.pojo.vo.*;
import com.kaili.basic.service.IMdmPushGroupService;
import com.kaili.common.util.ObjectUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @Desc: 推送组实现层
 * @author: TBS
 * @Time: 2019/8/1 18:53
 * @Copyright: © 2018 杭州凯立通信有限公司 版权所有
 * @Warning: 本内容仅限于公司内部传阅,禁止外泄或用于其它商业目的
 */

@Service
public class PushGroupServiceImpl implements IMdmPushGroupService {
    /**
     * 推送组mapper对象
     */
    @Autowired
    private PushGroupMapper mdmPushGroupMapper;

    /**
     * 分组mapper对象
     */
    @Autowired
    private GroupMapper mdmGroupMapper;

    /**
     * @param id
     * @return com.kaili.basic.pojo.entity.MdmPushGroupEntity
     * @Desc: 通过id查询推送组
     * @author: TBS
     * @Time: 2019/8/1 18:48
     */
    @Override
    public MdmPushGroupEntity queryPushGroupById(Integer id) {

        return mdmPushGroupMapper.queryPushGroupById(id);
    }

    /**
     * @param mdmOrgQueryDto
     * @return java.util.List<com.kaili.basic.pojo.entity.MdmOrgEntity>
     * @Desc: 条件查询机构
     * @author: TBS
     * @Time: 2019/8/1 18:48
     */
    @Override
    public List<MdmOrgEntity> queryOrgRanges(MdmOrgQueryDto mdmOrgQueryDto) {
        return mdmPushGroupMapper.queryOrgRanges(mdmOrgQueryDto);
    }

    /**
     * @param mdmGroupQueryDto
     * @return java.util.List<com.kaili.basic.pojo.entity.MdmGroupTree>
     * @Desc: 条件查询分组
     * @author: TBS
     * @Time: 2019/8/1 18:55
     */
    @Override
    public List<MdmGroupTree> queryGroupRanges(MdmGroupQueryDto mdmGroupQueryDto) {
        List<MdmGroupTree> list = mdmPushGroupMapper.queryGroupRanges(mdmGroupQueryDto);
        String parentStr = "";
        String parentIds[];
        List<MdmGroupTree> addList = new ArrayList<>();
        MdmGroupTree mdmGroupTree;
        MdmQueryGroupByIdVo mdmQueryGroupByIdVo;
        //将list里面的每个的父分组去重后添加到list里面来
        for (MdmGroupTree m : list
        ) {
            parentStr = mdmPushGroupMapper.queryGroupFather(Long.parseLong(m.getId()));
            parentIds = parentStr.split(",");
            for (String str : parentIds
            ) {
                if (!str.isEmpty() && !str.equals("0")) {
                    int i = 0;
                    int j = 0;
                    for (; i < list.size(); i++) {
                        if (list.get(i).getId().toString().equals(str)) break;
                    }
                    if (i < list.size()) continue;
                    else {
                        for (; j < addList.size(); j++) {
                            if (addList.get(j).getId().toString().equals(str)) break;
                        }
                        if (j < addList.size()) continue;
                        mdmQueryGroupByIdVo = mdmGroupMapper.queryGroupById(Integer.parseInt(str));
                        if (mdmQueryGroupByIdVo == null) continue;
                        mdmGroupTree = new MdmGroupTree();
                        mdmGroupTree.setId(mdmQueryGroupByIdVo.getId() + "");
                        mdmGroupTree.setGroupCode(mdmQueryGroupByIdVo.getGroupCode());
                        mdmGroupTree.setGroupName(mdmQueryGroupByIdVo.getGroupName());
                        mdmGroupTree.setParentId(mdmQueryGroupByIdVo.getParentId() + "");
                        addList.add(mdmGroupTree);//将这个父类增加到addList
                    }

                }
            }


        }
        list.addAll(addList);
        return list;
    }

    /**
     * @param
     * @return int
     * @Desc: 添加推送组
     * @author: TBS
     * @Time: 2019/8/1 18:49
     */
    @Override
    public void addPushGroup(MdmPushGroupEntity mdmPushGroupEntity) {
        mdmPushGroupMapper.addPushGroup(mdmPushGroupEntity);
    }

    /**
     * @param mdmPushGroupEntity
     * @return java.lang.Integer
     * @Desc: 推送组查重
     * @author: TBS
     * @Time: 2019/8/1 18:49
     */
    @Override
    public Integer querySupplierPushGroup(MdmPushGroupEntity mdmPushGroupEntity) {
        return mdmPushGroupMapper.querySupplierPushGroup(mdmPushGroupEntity);
    }

    /**
     * @param id
     * @return java.lang.String
     * @Desc: 通过id查询此分组的所有父分组
     * @author: TBS
     * @Time: 2019/8/1 18:49
     */
    @Override
    public String queryGroupFather(Long id) {
        return mdmPushGroupMapper.queryGroupFather(id);
    }

    /**
     * @return java.lang.String
     * @Desc: 设置code自增
     * @author: TBS
     * @Time: 2019/8/2 15:02
     */
    @Override
    public String getCode() {
        return mdmPushGroupMapper.getCode();
    }

    /***
     * @Desc: 修改推送组
     * @author: TBS
     * @Time: 2019/8/2 17:07
     * @param mdmPushGroupEntity
     * @return void
     */
    @Override
    public void update(MdmPushGroupEntity mdmPushGroupEntity) {
        mdmPushGroupMapper.update(mdmPushGroupEntity);
    }

    /**
     * @param mdmPushGroupQueryDto
     * @return com.baomidou.mybatisplus.plugins.Page<com.kaili.basic.pojo.vo.MdmPushGroupVO>
     * @Desc: 条件查询推送组
     * @author: TBS
     * @Time: 2019/8/5 15:13
     */
    @Override
    public Page<MdmPushGroupVO> queryPushGroup(Page<MdmPushGroupVO> page, MdmPushGroupQueryDto mdmPushGroupQueryDto) {
        List<MdmPushGroupVO> list = mdmPushGroupMapper.queryPushGroup(page, mdmPushGroupQueryDto);
        return page.setRecords(list);
    }

    /**
     * @param mdmPushGroupQueryDto
     * @return java.util.List<com.kaili.basic.pojo.vo.MdmPushGroupVO>
     * @Desc: 导出
     * @author: TBS
     * @Time: 2019/8/5 18:03
     */
    @Override
    public List<MdmPushGroupVO> export(MdmPushGroupQueryDto mdmPushGroupQueryDto) {
        return mdmPushGroupMapper.queryPushGroup(mdmPushGroupQueryDto);
    }

    /**
     * @param id
     * @return com.kaili.basic.pojo.vo.MdmPushGroupVO
     * @Desc: 根据id查询详情信息
     * @author: TBS
     * @Time: 2019/8/5 18:42
     */
    @Override
    public MdmPushGroupVO queryById(Integer id) {
        MdmPushGroupEntity entity = this.mdmPushGroupMapper.selectById(id);
        MdmPushGroupVO vo = mdmPushGroupMapper.queryById(
                new HashMap<String, Object>() {
                    {
                        this.put("id", id);
                        this.put("status", entity.getStatus());
                    }
                }
        );
        Integer[] integers = new Integer[1];
        List<MdmPushGroupByIdVo> list = new ArrayList<>();

        if (vo != null && vo.getGroupRangestr() != null) {
            String[] ids = vo.getGroupRangestr().split(",");
            integers = new Integer[ids.length];
            for (int i = 0; i < ids.length; i++) {
                if (ids[i].isEmpty()) continue;
                integers[i] = Integer.valueOf(ids[i]);
                list.add(new MdmPushGroupByIdVo(integers[i], this.mdmGroupMapper.queryGroupById(Integer.valueOf(ids[i])).getGroupName()));


            }
            if (!StringUtils.isEmpty(vo.getDeviceBelong())) {
                String[] belong = vo.getDeviceBelong().split(",");
                Integer[] belongIds = StringToInt(belong);
                vo.setDeviceBelongIds(belongIds);
            } else {
                vo.setDeviceBelongIds(new Integer[]{});
            }

            if (!StringUtils.isEmpty(vo.getDeviceType())) {
                String[] type = vo.getDeviceType().split(",");
                Integer[] typeIds = StringToInt(type);
                vo.setDeviceTypeIds(typeIds);
            } else {
                vo.setDeviceTypeIds(new Integer[]{});
            }

            if ((!StringUtils.isEmpty(vo.getDeviceTerrace()))) {
                String[] terrace = vo.getDeviceTerrace().split(",");
                Integer[] terraceIds = StringToInt(terrace);
                vo.setDeviceTerraceIds(terraceIds);
            } else {
                vo.setDeviceTerraceIds(new Integer[]{});
            }

            if (!StringUtils.isEmpty(vo.getDeviceModel())) {
                String[] model = vo.getDeviceModel().split(",");
                Integer[] modelIds = StringToInt(model);
                vo.setDeviceModelIds(modelIds);
            } else {
                vo.setDeviceModelIds(new Integer[]{});
            }

        }

        if (vo != null && vo.getGroupRangestr() != null) {
            vo.setGroupRangeIdsMap(list);
            vo.setGroupRange(integers);

        }
        return vo;
    }


    /**
     * @Desc:  根据推送组名称查询id和名称
     * @author: jianfeng
     * @Time: 2019/8/6 10:30
     * @param mdmPushGroupByNameDto
     * @return java.util.List<com.kaili.basic.pojo.vo.MdmPushGroupByNameVo>
     */
    @Override
    public List<MdmPushGroupByNameVo>  queryByName(MdmPushGroupByNameDto mdmPushGroupByNameDto) {
        return mdmPushGroupMapper.queryByName(ObjectUtils.objectToMap(mdmPushGroupByNameDto));
    }
    /**
    * @Desc:  删除推送组
    * @author: TBS
    * @Time: 2019/8/8 8:50
    * @param id
    * @return void
    */
    @Override
    public void delete(int id) {
        mdmPushGroupMapper.delete(id);
    }

    /*工具方法 String数组转Integer数组*/
    private Integer[]StringToInt(String[]arrs1){
        Integer[]ints=new Integer[arrs1.length];
            for(int i=0;i<arrs1.length;i++){
                ints[i]=Integer.parseInt(arrs1[i]);
            }
             return ints;
        }
}
